package in.muton.edushare.pages.assignment;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.AssignmentsDAO;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Enrolments;

import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.coursepages.CoursePage;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

public abstract class AssignmentBase extends EduShareBasePage {
	
	protected IModel<Assignments> model;
	protected static String[] menuItems = { "assnDetails", "assnSubmission",
											"assnCourse", "assnEdit"};
	public AssignmentBase(PageParameters params){
		add(CSSPackageResource.getHeaderContribution(AssignmentBase.class, "assnHome.css"));
		getAssignment(params);
		add(new Label("assnName", new PropertyModel<String>(this.model, "assignmentTitle")));
//		add(new EduShareURLPanel("courseLink", new PropertyModel<Courses>(this.model, "courseInstance.course")));
		BookmarkablePageLink<Void> courseLink = 
			new BookmarkablePageLink<Void>(
					"courseLink", 
					CoursePage.class, 
					EntityPageParams.getParams(new PropertyModel<Courses>(this.model, "courseInstance.course"))
					);
		courseLink.add(new Label("courseName", this.model.getObject().getCourseInstance().getCourse().getCourseName()));
		add(courseLink);
		
		CourseRoleCheck crc = new CourseRoleCheck(this.getModel().getObject().getCourseInstance(), getLoggedInUser());
		if(getModel().getObject().getStatus()==Assignments.ASSIGNMENT_STATUS_DRAFT&&!crc.isAdmin()&&!crc.isTeacher()){
			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.UNAUTHORIZED_PATH));
		}
		
		setupMenu();
	}

	private void getAssignment(PageParameters params) {
		int assignmentId = params.getInt("0", 0);
		if(assignmentId==0){
			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
		}
		Assignments assn = new AssignmentsDAO(getSessionORMInterface()).getAssignmentByAssignmentId(assignmentId);
		if(assn==null){
			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
		}
		setModel(new CayenneModel<Assignments>(assn));
		
	}

	private void setupMenu() {
		String highlight = getActivePage();
		for(String menuname: menuItems){
			WebMarkupContainer wmc = new WebMarkupContainer(menuname);
			if(menuname.equals(highlight)){
				wmc.add(new AttributeAppender("class", true, new Model<String>("es-selected"), " "));
			}
			
			if(menuname.equals("assnDetails")){
				wmc.add(new Link<Void>("link"){

					@Override
					public void onClick() {
						setResponsePage(Detail.class, EntityPageParams.getParams(AssignmentBase.this.getModel()));
					}
				});
			}else if(menuname.equals("assnSubmission")){
				wmc.add(new Link<Void>("link"){

					@Override
					public void onClick() {
						setResponsePage(Submissions.class, EntityPageParams.getParams(AssignmentBase.this.getModel()));
					}
				});
			}else if(menuname.equals("assnCourse")){
				wmc.add(new BookmarkablePageLink<Void>(
						"link", 
						CoursePage.class, 
						EntityPageParams.getParams(new PropertyModel<Courses>(this.model, "courseInstance.course"))
						));
			}else if(menuname.equals("assnEdit")){
				Link<Void> link = new Link<Void>("link"){
					
					@Override
					public void onClick() {
						setResponsePage(Edit.class, EntityPageParams.getParams(AssignmentBase.this.getModel()));
					}
				};
				
				CourseRoleCheck crc = new CourseRoleCheck(getModel().getObject().getCourseInstance(), getLoggedInUser());
				if(!crc.isTeacher() && !crc.isAdmin()){
//					link.setVisible(false);
					wmc.setVisible(false);
				}
				
				wmc.add(link);
			}
			add(wmc);
		}
	}

	protected abstract String getActivePage();

	@Override
	public String getUserRole() {
		
		return " : " + 
		Enrolments.getRoleAsString(
				CourseRoleCheck
				.getRole(this.model.getObject().getCourseInstance(), getLoggedInUser()));
	}

	public void setModel(IModel<Assignments> model) {
		this.model = model;
	}

	public IModel<Assignments> getModel() {
		return model;
	}

}
